package com.ithanwen.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ithanwen.mapper.EmpMapper;
import com.ithanwen.pojo.Emp;
import com.ithanwen.pojo.LoginInfo;
import com.ithanwen.service.LoginService;
import com.ithanwen.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created By tony
 * <br/>Date: 2025/8/13 09:16
 * <br/>Description:
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public LoginInfo login(String username, String password) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Emp emp = empMapper.selectOne(queryWrapper);
        if(emp == null){
            return null;
        }
        if(emp.getPassword().equals(password)){
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", emp.getUsername());
            claims.put("id", emp.getId());
            String token = JwtUtils.generateToken(claims);
            LoginInfo loginInfo = new LoginInfo(emp.getId(), emp.getUsername(), emp.getName(), token);
            return loginInfo;
        }
        return null;
    }
}
